%try: page
%except NameError: page = None
%end

%try: searchText
%except NameError: searchText = ''
%end

%try: sortColumn
%except NameError: sortColumn = ''
%end

%sort_func = lambda col: "window.location.href = '?page=%s&search_text=%s&sort=%s&asc=%s'" % (page, searchText, col, 1 if not asc or sortColumn != col else 0) if page else lambda col: ''
%th_class_func = lambda col: '' if not col == sortColumn else ('headerSortUp' if asc else 'headerSortDown')

%columns = ['Title', 'Author', 'Publisher', 'Type', 'Availability']

<table width="100%">
    <thead>
        <tr>
            %for col in columns:
                <th onclick="{{sort_func(col.lower())}}" class="{{th_class_func(col.lower())}}">{{col}}</th>
            %end
        </tr>
    </thead>
    <tbody>
    %for i, resource in zip(range(len(resources)), resources):
        <tr>
            <td onclick="$(this).find('.extraDetails').toggle()">
                <b style="cursor: pointer">{{resource.get('title', '').encode('utf-8')}}</b>
                <span class="extraDetails"><br />{{resource.get('summary', '').encode('utf-8') if resource.get('summary') else 'No further details available for this resource.'}}</span>
            </td>
            <td>{{resource.get('author', '').encode('utf-8')}}</td>
            <td>{{resource.get('publisher', '').encode('utf-8')}}</td>
            <td>{{resourceTypes[resource.get('resource_type_id', '')].encode('utf-8')}}</td>
            <td class="{{'in' if resourceAvailability[resource.get('id')] else 'out'}}">{{'Available' if resourceAvailability[resource.get('id')] else 'Checked Out'}}</td>
        </tr>
    %end
    </tbody>
    
    %if page:
        <tfoot>
            <td colspan="5">
                %if page > 1:
                    <a href="?page={{1}}&search_text={{searchText}}&sort={{sortColumn}}&asc={{asc}}">First</a>&nbsp;&nbsp;
                    <a href="?page={{page - 1}}&search_text={{searchText}}&sort={{sortColumn}}&asc={{asc}}">Previous</a>&nbsp;&nbsp;
                %end
                
                %for i in range(max(page - 5, 1), min(page + 5, max(2, numPages))):
                    %currentPage = page == i
                    %currentLink = '%s%d%s' % (('<a href="?page=%d&search_text=%s&sort=%s&asc=%s">' % (i, searchText, sortColumn, asc)) if not currentPage else '', i, '</a>' if not currentPage else '')
                    {{currentLink}}&nbsp;&nbsp;
                %end
                
                %lastPage = page == numPages
                %lastLink = '%s%d%s' % (('<a href="?page=%d&search_text=%s&sort=%s&asc=%s">' % (numPages, searchText, sortColumn, asc)) if not lastPage else '', numPages, '</a>' if not lastPage else '')
                
                of&nbsp;&nbsp;
                {{lastLink}}&nbsp;&nbsp;
                
                %if page < numPages:
                    <a href="?page={{page + 1}}&search_text={{searchText}}&sort={{sortColumn}}&asc={{asc}}">Next</a>&nbsp;&nbsp;
                    <a href="?page={{numPages}}&search_text={{searchText}}&sort={{sortColumn}}&asc={{asc}}">Last</a>
                %end
            </td>
        </tfoot>
    %end
</table>